package com.platform.modules.alg.alglib.poj2777;

public class Poj2777 {
/*    #define lc (rt)<<1
            #define rc (rt)<<1|1
            */

    private int maxn = 100010;
    private boolean ans[];
    private node tree[] = new node[maxn * 4];
    public String output = "";

    public Poj2777() {
        for (int i = 0; i < tree.length; i++) {
            tree[i] = new node();
        }
    }

    // 创建线段树
    void build(int rt, int l, int r) {
        tree[rt].l = l;
        tree[rt].r = r;
        if (rt != 1) tree[rt].color = 0;
        if (l == r) return;
        int mid = (l + r) >> 1;
        build((rt) << 1, l, mid);
        build((rt) << 1 | 1, mid + 1, r);
    }

    // 颜色统一
    void lift_up(int rt) {
        if (tree[(rt) << 1].color == tree[(rt) << 1 | 1].color)
            tree[rt].color = tree[(rt) << 1].color;
    }

    // 颜色下传
    void push_down(int rt) {
        if (tree[rt].color != 0) {
            tree[(rt) << 1].color = tree[(rt) << 1 | 1].color = tree[rt].color;
            tree[rt].color = 0;
        }
    }

    // 修改颜色
    void modify(int rt, int l, int r, int c) {
        if (tree[rt].l == l && tree[rt].r == r) {
            tree[rt].color = c;
            return;
        }
        push_down(rt);
        int mid = (tree[rt].l + tree[rt].r) >> 1;
        if (r <= mid) modify((rt) << 1, l, r, c);
        else if (l > mid) modify((rt) << 1 | 1, l, r, c);
        else {
            modify((rt) << 1, l, mid, c);
            modify((rt) << 1 | 1, mid + 1, r, c);
        }
        lift_up(rt);
    }

    // 查询
    void query(int rt, int l, int r) {
        if (tree[rt].color != 0) {
            ans[tree[rt].color] = true;
            return;
        }
        int mid = (tree[rt].l + tree[rt].r) >> 1;

        if (r <= mid) query((rt) << 1, l, r);
        else if (l > mid) query((rt) << 1 | 1, l, r);
        else {
            query((rt) << 1, l, mid);
            query((rt) << 1 | 1, mid + 1, r);
        }
    }

    public String cal(String input) {
        int n, t, o;
        String[] line = input.split("\n");
        String[] nums = line[0].split(" ");
        n = Integer.parseInt(nums[0]);
        t = Integer.parseInt(nums[1]);
        o = Integer.parseInt(nums[2]);

        tree[1].color = 1;
        build(1, 1, n);
        for (int i = 0; i < o; i++) {
            String[] command = line[i + 1].split(" ");
            int l, r;
            if (command[0].charAt(0) == 'C') {
                int c;
                l = Integer.parseInt(command[1]);
                r = Integer.parseInt(command[2]);
                c = Integer.parseInt(command[3]);
                modify(1, l, r, c);
            } else {
                ans = new boolean[50];
                l = Integer.parseInt(command[1]);
                r = Integer.parseInt(command[2]);
                query(1, l, r);
                int tot = 0;
                for (int k = 1; k <= t; k++)
                    if (ans[k])
                        tot++;
                output += tot + "\n";
            }
        }
        return output;
    }
}

class node {
    public int l;
    public int r;
    public int color;
}
